package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.AProcessRoute;
import com.ruoyi.system.mapper.AProcessRouteMapper;
import com.ruoyi.system.service.IAProcessRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工艺路线Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class AProcessRouteServiceImpl implements IAProcessRouteService 
{
    @Autowired
    private AProcessRouteMapper aProcessRouteMapper;

    /**
     * 查询工艺路线
     * 
     * @param id 工艺路线主键
     * @return 工艺路线
     */
    @Override
    public AProcessRoute selectAProcessRouteById(Long id)
    {
        return aProcessRouteMapper.selectAProcessRouteById(id);
    }

    /**
     * 查询工艺路线列表
     * 
     * @param aProcessRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    public List<AProcessRoute> selectAProcessRouteList(AProcessRoute aProcessRoute)
    {
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        aProcessRoute.setGroupId(deptId.toString());
        return aProcessRouteMapper.selectAProcessRouteList(aProcessRoute);
    }

    /**
     * 新增工艺路线
     * 
     * @param aProcessRoute 工艺路线
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAProcessRoute(List<AProcessRoute> aProcessRouteList)
    {
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        String routeId = IdUtils.fastSimpleUUID();
        for (AProcessRoute aProcessRoute : aProcessRouteList) {
            aProcessRoute.setGroupId(deptId.toString());
            aProcessRoute.setRouteId(routeId);
            aProcessRouteMapper.insertAProcessRoute(aProcessRoute);
        }
        return aProcessRouteList.size();
    }

    /**
     * 修改工艺路线
     * 
     * @param aProcessRoute 工艺路线
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateAProcessRoute(List<AProcessRoute> aProcessRouteList)
    {
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        AProcessRoute param = new AProcessRoute();
        param.setRouteId(aProcessRouteList.get(0).getRouteId());
        List<AProcessRoute> delList = aProcessRouteMapper.selectAProcessRouteList(param);
        List<Long> collect = delList.stream().map(AProcessRoute::getId).collect(Collectors.toList());
        //将list转为数组
        Long[] ids = collect.toArray(new Long[collect.size()]);
        aProcessRouteMapper.deleteAProcessRouteByIds(ids);
        for (AProcessRoute aProcessRoute : aProcessRouteList) {
            aProcessRoute.setGroupId(deptId.toString());
            aProcessRouteMapper.insertAProcessRoute(aProcessRoute);
        }
        return aProcessRouteList.size();
    }

    /**
     * 批量删除工艺路线
     * 
     * @param ids 需要删除的工艺路线主键
     * @return 结果
     */
    @Override
    public int deleteAProcessRouteByIds(Long[] ids)
    {
        return aProcessRouteMapper.deleteAProcessRouteByIds(ids);
    }

    /**
     * 删除工艺路线信息
     * 
     * @param id 工艺路线主键
     * @return 结果
     */
    @Override
    public int deleteAProcessRouteById(Long id)
    {
        return aProcessRouteMapper.deleteAProcessRouteById(id);
    }

    @Override
    public int deleteAProcessRouteByRouteId(String routeId) {
        if(StringUtils.isEmpty(routeId)){
            return 0;
        }
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        AProcessRoute param = new AProcessRoute();
        param.setRouteId(routeId);
        param.setGroupId(deptId.toString());
        List<AProcessRoute> delList = aProcessRouteMapper.selectAProcessRouteList(param);
        List<Long> collect = delList.stream().map(AProcessRoute::getId).collect(Collectors.toList());
        //将list转为数组
        Long[] ids = collect.toArray(new Long[collect.size()]);
        aProcessRouteMapper.deleteAProcessRouteByIds(ids);
        return delList.size();
    }
}
